using System;

namespace PcapDotNet.Base
{
    /// <summary>
    /// Extension methods for Func of type T.
    /// </summary>
    public static class FuncExtensions
    {
        /// <summary>
        /// Generates an array of a given size by generating elements using the given delegate.
        /// </summary>
        /// <typeparam name="T">The type of the array to create.</typeparam>
        /// <param name="generator">The delegate to generate elements with.</param>
        /// <param name="size">The size of the array to create.</param>
        /// <returns>An array of a given size with elements generated by the given delegate.</returns>
        public static T[] GenerateArray<T>(this Func<T> generator, int size)
        {
            if (generator == null) 
                throw new ArgumentNullException("generator");

            T[] array = new T[size];
            for (int i = 0; i != size; ++i)
                array[i] = generator.Invoke();
            return array;
        }
    }
}